#!/usr/bin/env python
# --*-- coding:utf-8 --*--
# author:g-y-b time:2021/4/13

import cv2 as cv

# 获取一个视频并打开
cap = cv.VideoCapture('./image/DOG.mp4')

if cap.isOpened():  # VideoCapture对象是否成功打开
    print('已经打开了视频文件')
    fps = cap.get(cv.CAP_PROP_FPS)  # 返回视频的fps--帧率
    width = cap.get(cv.CAP_PROP_FRAME_WIDTH)    # 返回视频的宽
    height = cap.get(cv.CAP_PROP_FRAME_HEIGHT)  # 返回视频的高
    print(f"fps:{fps}, width:{width}, height:{height}")

    i = 0
    while 1:
        if i == 15:
            print('保存了视频的前15帧图像，保存结束')
            break
        else:
            i += 1
            ret, frame = cap.read()     # 读取一帧视频
            # ret 读取了数据就返回True，没有读取数据（已到尾部）就返回False
            #  frame 返回读取的视频数据--一帧数据
            file_name = './dog/img' + str(i) + '.jpg'
            # print(file_name)
            cv.imwrite(file_name, frame)

else:
    print('视频打开失败')